package com.biubiux2.message;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.PooledByteBufAllocator;
import io.netty.buffer.UnpooledByteBufAllocator;

import java.nio.charset.StandardCharsets;

/**
 * @author noexcs
 * @since 8/7/2022 4:59 PM
 */
public class MessageUtil {

    public static ByteBuf createLoginMsg(int userId, String password) {
        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
        buffer.writeInt(MessageType.LOGIN_REQUEST);
        buffer.writeInt(userId);
        buffer.writeBytes(password.getBytes(StandardCharsets.UTF_8));
        return buffer;
    }

    public static ByteBuf createLogoutMsg() {
        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
        buffer.writeInt(MessageType.LOGOUT);
        buffer.writeBytes("LOGOUT".getBytes(StandardCharsets.UTF_8));
        return buffer;
    }

    public static ByteBuf createSendMsg(int senderUserId, int receiverUserId, String content) {
        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
        buffer.writeInt(MessageType.USER_MSG);
        buffer.writeInt(senderUserId);
        buffer.writeInt(receiverUserId);
        buffer.writeBytes(content.getBytes(StandardCharsets.UTF_8));
        return buffer;
    }

    public static ByteBuf createReconnectMsg(String sessionId) {
        ByteBuf buffer = PooledByteBufAllocator.DEFAULT.buffer();
        buffer.writeInt(MessageType.RECONNECT);
        buffer.writeBytes(sessionId.getBytes(StandardCharsets.UTF_8));
        return buffer;
    }

}
